﻿using Microsoft.AspNetCore.Mvc;
using ProjectScheduling_web.dao;
using ProjectScheduling_web.model;

namespace ProjectScheduling_web.Controllers.U
{
    public class InvoiceController : BController
    {
        [HttpGet]
        public IActionResult Search(int page, int row, string? key, string? startTime, string? endTime)
        {
            var list = InvoiceDao.Base.Search(page, row, key, startTime, endTime);
            return Success(list);
        }
        [HttpPost]
        public IActionResult Update([FromBody] Invoice req)
        {
            if (req == null) return Fail("参数错误");
            if (string.IsNullOrEmpty(req.Name)) return Fail("开票名称不能为空");
            #region 验证客户
            if (req.CustomerId == 0) return Fail("客户不能为空");
            var cus = CustomerDao.Base.Find(req.CustomerId);
            if (cus == null) return Fail("客户不存在");
            req.CustomerName = cus.Name;
            #endregion
            #region 验证发票类型
            if (req.InvoiceTypeId == 0) return Fail("发票类型不能为空");
            var invoiceType = DictionaryDataDao.Base.Find(it => it.Category == model.Enums.EnumDictionaryCategory.发票类型 && it.Value == req.InvoiceTypeId);
            req.InvoiceTypeName = invoiceType?.Name;
            #endregion
            if (req.Money <= 0) return Fail("开票金额必须大于0");
            #region 验证开票人
            if (req.TickerId == 0) return Fail("开票人不能为空");
            var ticker = MemberDao.Base.Find(z => z.Id == req.TickerId);
            if (ticker == null) return Fail("开票人不存在");
            req.TickerName = ticker.Name;
            #endregion
            #region 验证归属部门
            if (req.OrgId == 0) return Fail("所属部门不能为空");
            var org = DictionaryDataDao.Base.Find(it => it.Category == model.Enums.EnumDictionaryCategory.部门 && it.Value == req.OrgId);
            req.OrgName = org?.Name;
            #endregion
            if (req.Id > 0)
            {
                InvoiceDao.Base.Update(req, it => new
                {
                    it.Name,
                    it.InvoiceDate,
                    it.CustomerId,
                    it.CustomerName,
                    it.InvoiceTypeId,
                    it.InvoiceTypeName,
                    it.Money,
                    it.TickerId,
                    it.TickerName,
                    it.OrgId,
                    it.OrgName,
                    it.TaxNumber
                });
                InvoiceProductDao.Base.Delete(it => it.InvoiceId == req.Id);
                if (req.Products != null && req.Products.Count > 0)
                {
                    foreach (var item in req.Products)
                    {
                        item.InvoiceId = req.Id;
                        item.CreaterId = CurrUser?.Id ?? 0;
                        item.CreaterName = CurrUser?.Name;
                        item.CreateTime = DateTime.Now;
                        InvoiceProductDao.Base.Add(item);
                    }
                }
                FileMsgDao.Base.Delete(it => it.PId == req.Id && it.MsgType == model.Enums.EnumMsgType.开票);
                if (req.Files != null && req.Files.Count > 0)
                {
                    foreach (var item in req.Files)
                    {
                        item.PId = req.Id;
                        item.MsgType = model.Enums.EnumMsgType.开票;
                        FileMsgDao.Base.Add(item);
                    }
                }
                return Success("更新成功");
            }
            else
            {
                req.CreateTime = DateTime.Now;
                if (CurrUser != null)
                {
                    req.CreaterId = CurrUser.Id;
                    req.CreaterName = CurrUser.Name;
                }
                var id = InvoiceDao.Base.Add(req);
                if (req.Products != null && req.Products.Count > 0)
                {
                    req.Products.ForEach(item =>
                    {
                        item.InvoiceId = id;
                        item.CreaterId = CurrUser?.Id ?? 0;
                        item.CreaterName = CurrUser?.Name;
                        item.CreateTime = DateTime.Now;
                        InvoiceProductDao.Base.Add(item);
                    });
                }
                if (req.Files != null && req.Files.Count > 0)
                {
                    foreach (var item in req.Files)
                    {
                        item.PId = id;
                        item.MsgType = model.Enums.EnumMsgType.开票;
                        FileMsgDao.Base.Add(item);
                    }
                }
                return Success("创建成功");
            }
        }
        [HttpGet]
        public IActionResult Find(int id)
        {
            return Success(InvoiceDao.Base.Detail(id));
        }
        [HttpGet]
        public IActionResult Remove(int id)
        {
            InvoiceDao.Base.Remove(id);
            return Success("删除成功");
        }
    }
}
